package org.gcb.core.sandbox;

import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.InputEvent;
import java.io.IOException;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.TransferHandler;
import org.gcb.core.model.Item;
import org.gcb.core.model.Toon;
import org.openide.util.Exceptions;

/**
 *
 * @author Jason
 */
public class ItemFromTransferHandler extends TransferHandler{
    private Item item;
    private Toon toon;

    public ItemFromTransferHandler(Toon toon) {
	System.out.println("hello");
	this.toon = toon;
    }

    public ItemFromTransferHandler(Toon toon, String property) {
	super(property);
	System.out.println("hello property");
	this.toon = toon;
    }

    @Override
    public void exportAsDrag(JComponent comp, InputEvent e, int action) {
	int ea = 3;
	ea+=4;
	super.exportAsDrag(comp, e, action);
    }

    @Override
    protected Transferable createTransferable(JComponent c) {
	Transferable transferable = super.createTransferable(c);
	return transferable;
    }

    @Override
    protected void exportDone(JComponent source, Transferable data, int action) {
	try {
	    item = Item.class.cast(data.getTransferData(Item.ITEM_DATA_FLAVOR));
	} catch (UnsupportedFlavorException ex) {
	    Exceptions.printStackTrace(ex);
	} catch (IOException ex) {
	    Exceptions.printStackTrace(ex);
	}
	toon.getEquipment().getCarried().remove(item);
	super.exportDone(source, data, action);
    }

    @Override
    public int getSourceActions(JComponent c) {
	return MOVE;
    }

    @Override
    public Icon getVisualRepresentation(Transferable t) {
	try {
	    return Item.class.cast(t.getTransferData(Item.ITEM_DATA_FLAVOR)).getIcon();
	} catch (UnsupportedFlavorException ex) {
	    Exceptions.printStackTrace(ex);
	} catch (IOException ex) {
	    Exceptions.printStackTrace(ex);
	}
	return super.getVisualRepresentation(t);
    }

}
